/*
 * EasyBounceGameContainer.cpp
 *
 *  Created on: 2009-3-14
 *      Author: Wayne
 */

#include <aknviewappui.h>
#include <aknnotewrappers.h>
#include "EasyBounceGameContainer.h"
#include <eikbtgpc.h>
#include "coedef.h"
#include <EasyBounce.rsg>
#include <easybounceballs.mbg>
#include "Ball.h"
#include "Engine.h"
#include "EasyBounceGameView.h"

_LIT(KBmpFile, "\\resource\\apps\\easybounceballs.mbm");

// ================= MEMBER FUNCTIONS =======================

CEasyBounceGameContainer::CEasyBounceGameContainer()
	{
	iWoodenBall = NULL;
	iBall = iBallMask = NULL;
	}

// ---------------------------------------------------------
// ConstructL(const TRect& aRect)
// Two phased constructor
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::ConstructL()
    {
    CreateWindowL();
	const TDesC& file = KBmpFile;
	AknIconUtils::CreateIconL(iBall, iBallMask, file, 
				(TInt)EMbmEasybounceballsWooden_ball, (TInt)EMbmEasybounceballsWooden_ball_mask);
	iWoodenBall = new (ELeave)CEikImage;
	iWoodenBall->SetPicture( iBall, iBallMask );
	iWoodenBall->SetAlignment( EHCenterVCenter );
	iWoodenBall->SetExtent( TPoint(168,40), TSize(24,24) );
	iBgContext = CAknsBasicBackgroundControlContext::NewL( 
	   			KAknsIIDQsnBgAreaMain, Rect(), ETrue);
    ActivateL();
    }

CEasyBounceGameContainer* CEasyBounceGameContainer::NewL()
	{
	CEasyBounceGameContainer* self = new (ELeave)CEasyBounceGameContainer;
	CleanupStack::PushL( self );
	self->ConstructL();
	CleanupStack::Pop();
	return self;
	}

// Destructor
CEasyBounceGameContainer::~CEasyBounceGameContainer()
    {
	delete  iWoodenBall;
	iWoodenBall = NULL;
	iBall = NULL;
	iBallMask = NULL;
	delete iBgContext;
	iBgContext = NULL;
    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::SizeChanged()
    {
    if(iBgContext)
		{
		iBgContext->SetRect(Rect());
        if ( &Window() )
			{
			iBgContext->SetParentPos( PositionRelativeToScreen() );
			}
		}
	CaculateLayout();
    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CEasyBounceGameContainer::CountComponentControls() const
    {
    return ( int ) ELastControl; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CEasyBounceGameContainer::ComponentControl(TInt aIndex) const
    {
	switch ( aIndex )
		{
		case EWoodenBallImage:
			{
			return iWoodenBall;
			}
		default:
			return NULL;
		}
    }


// ---------------------------------------------------------
// CEasyBounceGameContainer::OffeKeyEventL()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
TKeyResponse CEasyBounceGameContainer::OfferKeyEventL(
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
    {
    TKeyResponse ret;

    ret = EKeyWasNotConsumed;

    return ret;
    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::Draw(const TRect& aRect) const
    {
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	
    MAknsSkinInstance* skin = AknsUtils::SkinInstance();
    MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
    AknsDrawUtils::Background( skin, cc, this, gc, aRect );
    gc.SetPenColor( KRgbDarkCyan );
    gc.SetPenSize( TSize(1,4) );
    gc.DrawLine( iLineStart, iLineEnd );

    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::HandleControlEventL(
    CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    // Add your control event handler code here
    }

// ---------------------------------------------------------
// CEasyBounceGameContainer::HandlePointerEventL(
//     const TPointerEvent& aPointerEven)
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::HandlePointerEventL( const TPointerEvent& aPointerEvent )
	{
	CEngine* game = CEngine::Instance();
	TRect modelRect = game->GetGameRoom();
	TRect viewRect = Rect();
    switch( aPointerEvent.iType )
    	{
    	case TPointerEvent::EButton1Down:
    		{
    		TInt xx = ( aPointerEvent.iPosition.iX * modelRect.Width()) / viewRect.Width();
    		TInt yy = (( viewRect.Height() - aPointerEvent.iPosition.iY ) * modelRect.Height()) / viewRect.Height();
			game->GetLine()->SetStart( TPoint( xx,yy ) );
			game->GetLine()->SetEnd( TPoint( xx,yy ) );
    		break;
    		}
    	case TPointerEvent::EButton1Up:
    		{
    		TInt xx = ( aPointerEvent.iPosition.iX * modelRect.Width()) / viewRect.Width();
    		TInt yy = (( viewRect.Height() - aPointerEvent.iPosition.iY ) * modelRect.Height()) / viewRect.Height();
    		game->GetLine()->SetEnd( TPoint( xx, yy) );
    		break;
    		}
    	}
	}

// ---------------------------------------------------------
// CEasyBounceGameContainer::GameChanged(
//     CEngine* aGame)
// ---------------------------------------------------------
//
void CEasyBounceGameContainer::GameChanged( CEngine* /*aGame*/ )
	{
	CaculateLayout();
	DrawDeferred();
	}

void CEasyBounceGameContainer::GameOver( CEngine* /*aGame*/ )
	{
	CAknInformationNote* note = new (ELeave)CAknInformationNote( EFalse );
	note->ExecuteLD(_L("Game Over!"));
	CaculateLayout();
	DrawDeferred();
	}

void CEasyBounceGameContainer::GamePaused( CEngine* /*aGame*/ )
	{
	
	}

void CEasyBounceGameContainer::HandleResourceChange( TInt /*aType*/ )
	{
	CAknViewAppUi* appui = static_cast<CAknViewAppUi*>( iEikonEnv->AppUi() );
	appui->View(KGameViewId)->HandleViewRectChange();
	}

void CEasyBounceGameContainer::CaculateLayout()
	{
	CEngine* game = CEngine::Instance();
	TRect modelRect = game->GetGameRoom();
	TRect viewRect = Rect();
	CBall* ball = game->GetBall();
	TInt xx = ( (ball->GetPos().iX + 0.5f) * viewRect.Width()) / modelRect.Width();
	TInt yy = viewRect.Height() - ( (ball->GetPos().iY + 0.5f) * viewRect.Height()) / modelRect.Height();
		
	iWoodenBall->SetExtent( TPoint(xx -12 ,yy -12 ), iWoodenBall->Size() );
	iLineStart.iX = ( game->GetLine()->GetStart().iX * viewRect.Width()) / modelRect.Width();
	iLineStart.iY = viewRect.Height() - ( game->GetLine()->GetStart().iY * viewRect.Height()) / modelRect.Height();
	iLineEnd.iX = ( game->GetLine()->GetEnd().iX * viewRect.Width()) / modelRect.Width();
	iLineEnd.iY = viewRect.Height() - ( game->GetLine()->GetEnd().iY * viewRect.Height()) / modelRect.Height();	
	}

// ---------------------------------------------------------
// CEasyBounceGameContainer::MopSupplyObject()
// Needed for supporting skin     
// ---------------------------------------------------------
//
TTypeUid::Ptr CEasyBounceGameContainer::MopSupplyObject( TTypeUid aId )
	{
	if (iBgContext )
		 {
		   return MAknsControlContext::SupplyMopObject( aId, iBgContext );
		 }
	return CCoeControl::MopSupplyObject(aId);
	}


// End of File  
